import _ from 'lodash';
import feed from '@/components/FeedBackAlert';
import { checkFeedList, updateFeed } from '@/services/analytics';
import { getToken } from '@/utils/authority';
import { HttpCode } from '@/utils/contacts';
// import { upInfo, getNoticeMessageInfo } from '@/services/eam';
// import NoticeMessageAlert from '@/components/NoticeMessageAlert';
// import ProgramUpdateAlert from '@/components/ProgramUpdateAlert';

const FEED_KEY = 'feedKey';

class Jurisdiction {
  constructor() {
    this.PERMISSION_ARRAY = [];
    this.FEEDBACK_LIST = [];
  }
  save(list = []) {
    this.PERMISSION_ARRAY = _.unionWith(list);
  }
  getMenuItem(pathname = '') {
    return this.PERMISSION_ARRAY.find((n) => pathname === `/${n.component}`);
  }
  getMenuCode(pathname = '') {
    return this.getMenuItem(pathname)?.menuCode ?? '';
  }
  saveFeed(list = []) {
    this.FEEDBACK_LIST = [...list];
  }
  updateFeedById(bId) {
    this.FEEDBACK_LIST = this.FEEDBACK_LIST.filter((n) => n.id !== bId);
  }
  async onUserCheckFeedList(isCheckFeed = true) {
    // let upData = await upInfo();  //暂时注释
    // if (upData.show) {
    //   ProgramUpdateAlert.show();
    //   NoticeMessageAlert.dismiss();
    //   return;
    // }
    // const userId = getDvaApp()?._store?.getState()?.user?.currentUser?.personId;
    // let messageInfo = await getNoticeMessageInfo({ userId });
    // if (messageInfo.show) {
    //   NoticeMessageAlert.show(messageInfo.data);
    //   return;
    // }
    // ProgramUpdateAlert.dismiss();
    // NoticeMessageAlert.dismiss();
    if (!isCheckFeed) return;
    let retraction = await checkFeedList();
    if (retraction?.statusCode === HttpCode.SUCCESS) {
      this.saveFeed(retraction?.checkPlatformFeedback);
    }
  }
  async onCheckFeed(menuCode) {
    try {
      let arr = localStorage.getItem(FEED_KEY);
      arr = !arr ? [] : JSON.parse(arr);
      const array = this.FEEDBACK_LIST.filter((n) => {
        if (n?.twoMenu) {
          if (n?.twoMenu === menuCode) return true;
          return false;
        }
        if (n?.oneMenu === menuCode) return true;
        return false;
      });
      if (array.length > 0 && feed.__key__.length === 0) {
        const fistItem = array[0];
        if (!arr.includes(fistItem?.id || 0)) {
          feed.show(fistItem);
        } else {
          const token = await getToken();
          if ((token?.length ?? 0) === 0) {
            return;
          }
          updateFeed({ platformFeedbackId: fistItem.id, checksum: '1' });
        }
        this.updateFeedById(fistItem?.id);
      }
    } catch (e) {}
  }
}

Jurisdiction.getInstance = (function () {
  let instance;
  return function () {
    instance = instance ? instance : new Jurisdiction();
    return instance;
  };
})();

const jurisdiction = Jurisdiction.getInstance();

export default jurisdiction;
